package com.lrkj.property.service.impl;

import com.lrkj.property.domain.Employee;
import com.lrkj.property.mapper.EmployeeMapper;
import com.lrkj.property.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工关联表Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-03-16
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 查询员工关联表
     *
     * @param userId 员工关联表ID
     * @return 员工关联表
     */
    @Override
    public Employee selectEmployeeById(Long userId) {
        return employeeMapper.selectEmployeeById(userId);
    }

    /**
     * 查询员工关联表列表
     *
     * @param employee 员工关联表
     * @return 员工关联表
     */
    @Override
    public List<Employee> selectEmployeeList(Employee employee) {
        return employeeMapper.selectEmployeeList(employee);
    }

    /**
     * 新增员工关联表
     *
     * @param employee 员工关联表
     * @return 结果
     */
    @Override
    public int insertEmployee(Employee employee) {
        if (employee.getUserIds().length > 0) {
            List<Long> newUserIds = Arrays.asList(employee.getUserIds());
            //已经存在的IDS
            List<Long> exisUserIds = employeeMapper.exisUserId(employee);
            //前台传过来的 用户IDS - 已经存在的用户IDS
            List<Long> savaUserIds = newUserIds.stream().filter(userId -> !exisUserIds.contains(userId+""))
                    .collect(Collectors.toList());
            List<Employee> savaEmployees = new ArrayList<>();
            savaUserIds.stream().forEach(userId -> {
                Employee bean = new Employee();
                bean.setBusinessId(employee.getBusinessId());
                bean.setBusinessType(employee.getBusinessType());
                bean.setUserId(userId);
                savaEmployees.add(bean);
            });
            this.doSaveList(savaEmployees);
        }
        return 1;
    }

    /**
     * 修改员工关联表
     *
     * @param employee 员工关联表
     * @return 结果
     */
    @Override
    public int updateEmployee(Employee employee) {
        return employeeMapper.updateEmployee(employee);
    }

    /**
     * 批量删除员工关联表
     *
     * @param employee 需要删除的员工关联表ID
     * @return 结果
     */
    @Override
    public int deleteEmployeeByIds(Employee employee) {
        return employeeMapper.deleteEmployeeByIds(employee);
    }

    /**
     * 删除员工关联表信息
     *
     * @param userId 员工关联表ID
     * @return 结果
     */
    @Override
    public int deleteEmployeeById(Long userId) {
        return employeeMapper.deleteEmployeeById(userId);
    }

    /**
     * 批量新增员工关联表信息
     *
     * @param employees 员工关联表对象
     */
    public int batchAddData(List<Employee> employees) {
        return employeeMapper.batchAddData(employees);
    }
}
